package com.crawler.utils;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class ImageDownloadPipelineUtils implements Pipeline
{

    private String downloadPath = "downloads/";

    public ImageDownloadPipelineUtils(String path) {
        this.downloadPath = path;
    }

    @Override
    public void process(ResultItems resultItems, Task task) {
        String downloadLink = resultItems.get("downloadLink");
        if (downloadLink != null) {
            try {
                URL url = new URL(downloadLink);
                String fileName = Paths.get(url.getPath()).getFileName().toString();
                Path outputPath = Paths.get(downloadPath, fileName);

                // 创建目录
                Files.createDirectories(outputPath.getParent());

                // 下载文件
                try (InputStream in = url.openStream()) {
                    Files.copy(in, outputPath, StandardCopyOption.REPLACE_EXISTING);
                    System.out.println("下载完成: " + fileName);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
